/*********************************************************************************
 *      Copyright:  (C) 2020 Juan
 *                  All rights reserved.
 *
 *       Filename:  client.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(14/07/20)
 *         Author:  wangruijie <1046299465@qq.com>
 *      ChangeLog:  1, Release initial version on "14/07/20 13:47:24"
 *                 
 ********************************************************************************/
#include"json_client.h"
#include"cJSON.h"

int g_stop = 0;

void sighandler(int sig_num)
{
    if(sig_num == SIGINT)
    {
        g_stop = 1;
    }
}

int main(int argc, char **argv) 
{
    int                     conn_fd = -1; 
    int                     rv = -1; 
    char                    json_buf[1024]; 
    sqlite3                 *db;
    char                    *sqlite_name;
    struct sockaddr_in      serv_addr;
    int                     SERVER_PORT;
    char                    *SERVER_IP = "127.0.0.1";
    char                    *progname = NULL;
    int                     opt;

    struct option           opts[] = {
        {"port", required_argument, NULL, 'p'},
        {"ip", required_argument, NULL, 'L'},
        {"help", no_argument, NULL, 'h'},
        {NULL, 0, NULL, 0}
    };

    while( (opt = getopt_long(argc, argv, "p:h", opts, NULL)) != -1)
    {
        switch(opt)
        {
            case 'p':
                SERVER_PORT = atoi(optarg);
                break;

            /*case 'L':
                SERVER_IP[1024] = atoi(optarg);
                break;*/

            case 'h':
                print_usage(argv[0]);
                return 0;
        }
    }

    if(!SERVER_PORT && SERVER_IP)
    {
        print_usage(argv[0]);
        return 0;
    }


    signal(SIGINT, sighandler);      //安装信号

    if((db = sqlite_init(sqlite_name)) == NULL)   //初始化数据库
    {
        printf("Can not use sqlite3\n");
        return -1;
    }
    
    memset(json_buf, 0, strlen(json_buf));
    while(!g_stop)
    {
        conn_fd = socket(AF_INET, SOCK_STREAM, 0); 
        if(conn_fd < 0) 
        {
            printf("create socket failure: %s\n", strerror(errno)); 
            return -1;
        }

        memset(&serv_addr, 0, sizeof(serv_addr)); 
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_port = htons(SERVER_PORT);
        inet_aton( SERVER_IP, &serv_addr.sin_addr ); 

        if( connect(conn_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))  < 0) 
        {
            printf("connect to server [%s:%d] failure: %s\n", SERVER_IP, SERVER_PORT, strerror(errno)); 
            return 0;; 
        }

        rv = pack_json(json_buf);
        if(rv < 0)
        {
            printf("pack json failure:%s\n", strerror(errno));
            return -1;
        }
        printf("data:%s\n", json_buf);

        if( write(conn_fd, json_buf, strlen(json_buf)) < 0 ) 
        {
            printf("Write data to server [%s:%d] failure: %s\n", SERVER_IP, SERVER_PORT, strerror(errno)); 
            goto SAVE_DB;
        }
        memset(json_buf, 0, sizeof(json_buf));

        rv = get_data_from_database(db,json_buf);  //  查询数据库中的数据  
        if( rv < 0)
        {
            printf("get data from sqlite failure:%s\n", strerror(errno));
            return -2;
        }

        rv = write(conn_fd, json_buf, strlen(json_buf)); /*  发送数据库中的历史数据  */
        if(rv == 0)
        {
            if(!delete_data_from_database(db))   //删除已经发送的数据库中的内容
            {
                printf("delete data in sqlite\n");
                return -2;
            }
            else
            {
                printf("delete data from sqlite failure:%s\n", strerror(errno));
            }
        }
        else
        {
            printf("send data to server from sqlite failure:%s\n", strerror(errno));
            return -4;
        }

        close(rv);
        close(conn_fd);

SAVE_DB:
        rv = save_data_in_database(db,json_buf);
        if(rv < 0)
        {
            printf("Can't use database!\n");
            return -2;
        }
    }
}

